


function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    };
    return 0;
 }


if ($('input[name="ref"][value="cs_503_search"]').length) {
    console.log('503 拦截 sleep');
    setTimeout(() => {
           location.reload()
    },100000);
} else if(getQueryString("auto_get")=='true'){

    var data = [];
    console.log("get data start");
    console.log( $(".s-card-container").html());
    var data = [];
     $(".s-card-container").each(function(e){
            if($(this).find(".puis-label-popover-default").html()){
                return;
            }
            console.log("index--");
            var indexid=$(this).attr("id")+"";
            var dataItem = {};
            dataItem.url = $(this).find("a").attr("href");
            dataItem.title = $(this).find(".a-size-medium ").html();
            if(!dataItem.title){
                dataItem.title =  $(this).find(".a-size-base-plus").html();
            }
            dataItem.price = $(this).find(".a-offscreen").html();
            dataItem.img = $(this).find("img").attr("src");
            dataItem.comment = $(this).find(".a-size-base.s-underline-text").html();
            console.log(dataItem);
            data.push(dataItem);
     });
    console.log(data);
    console.log(data.length );
    var fetchNextCate = false;
     if(data && data.length > 0){
      console.log("have data ");
        var type= "getamazon";
        var q = getQueryString("k");
        var page = parseInt(getQueryString("page"));
        if(page<=0){
            page = 1;
        }
        var skip_fetch_detail = true;

        chrome.runtime.sendMessage({ data: {type,data,page,q}, close: false ,skip_fetch_detail:skip_fetch_detail});
        if(skip_fetch_detail){
             if(page < 50){
                    setTimeout(() => {
                        // 获取当前URL
                        var url = window.location.href;
                        // 判断 URL 是否包含 page=xx
                        if (url.includes('page=')) {
                          // 使用正则表达式替换字符串
                          url = url.replace(/(page=)(\d+)/g, function(match, p1, p2) {
                            return p1 + (page+1); // 将页码加 1
                          });
                        } else {
                          // 拼接 page=1
                          url += (url.includes('?') ? '&' : '?') + 'page=2';
                        }

                        // 将参数列表拼接为新的 URL
                        console.log(url);
                        // 使用新的 URL 进行页面跳转
                        location.href = url;
                },10000);
            }else{
                fetchNextCate = true;
            }

        }

     }else{
       fetchNextCate = true;

     }

   if(fetchNextCate == true){
        console.log("no data ");
        var type= "getamazonend";
        var q = getQueryString("k");
         setTimeout(function() {
                     chrome.runtime.sendMessage({ data: {type,data,page,q}, close: true });
        }, 10000); // 20秒钟后关闭标签页

   }



       /* chrome.runtime.onMessage.addListener(async (msg, sender) => {
            console.log(" jump page ")
            console.log(msg)
            if(msg && parseInt(msg) > 50){
                var type= "getamazonend";
                var q = getQueryString("k");
                chrome.runtime.sendMessage({ data: {type,data,page,q}, close: true });
                return;
            }
            // 获取当前URL
                var url = window.location.href;
                // 判断 URL 是否包含 page=xx
                if (url.includes('page=')) {
                  // 使用正则表达式替换字符串
                  url = url.replace(/(page=)(\d+)/g, function(match, p1, p2) {
                    return p1 + (msg); // 将页码加 1
                  });
                } else {
                  // 拼接 page=1
                  url += (url.includes('?') ? '&' : '?') + 'page=2';
                }
                console.log("jump_to_url_"+url);
                location.href = url;
        });*/

}



